package com.xidian.zhiancloud.controller.equipFacility.preUnit;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.PuOut;
import com.xidian.zhiancloud.model.WarehouseOut;
import com.xidian.zhiancloud.qo.equipFacility.PuOutQO;
import com.xidian.zhiancloud.service.equipFacility.PuInService;
import com.xidian.zhiancloud.service.equipFacility.PuOutService;
import com.xidian.zhiancloud.vo.emergencyManagement.WarehouseOutVO;
import com.xidian.zhiancloud.vo.equipFacility.PuOutVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 备品备件出库
 */
@RestController
@RequestMapping("/PuOut")
public class PuOutController extends BaseController {
    @Autowired
    private PuOutService puOutService;

    public int count = 0;
    public Date date = new Date();

    /**
     * 增加出库记录
     *
     * @param puOut 出库对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(PuOut puOut) {
        puOut.setChuTime(new Date(System.currentTimeMillis()));
        puOut.setChuer(getSessionUserid());
        puOut.setCompanyId(getSessionUserCompanyId());
        Date temp = new Date();
        if (count == 0) {
            date = temp;
        }
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(temp);
        cal2.setTime(date);
        if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) &&
                cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)) {
            count++;
        } else {
            count = 0;
        }
        String str = cal1.get(Calendar.YEAR) + String.format("%02d", cal1.get(Calendar.MONTH) + 1) +
                String.format("%02d", cal1.get(Calendar.DAY_OF_MONTH)) + String.format("%03d", count);
        puOut.setCode(str);
        Boolean res = puOutService.add(puOut);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }
    /**
     * 获取出库记录
     * @param puOutQO 出库记录对象
     * @return 出库对象
     */
    @GetMapping("/getPuOut")
    public  ResultDTO<IPage<PuOutVO>> getPuOut(PuOutQO puOutQO){
        if (puOutQO.getCurrent() == null || puOutQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        puOutQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(puOutService.getPuOut(puOutQO),GeneralResponseEnums.SUCCESS);
    }
}
